package vue;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import modele.Utilisateur;

public class PanelClassement extends JPanel{
	
	private JTable table;
	private DefaultTableModel model;
	private JScrollPane scroll;
	private FenetrePrincipale ihm;
	private int nbLigneRemplies = 0;
	
	public PanelClassement(FenetrePrincipale fe)
	{
		this.ihm = fe;
		this.setBorder(BorderFactory.createEmptyBorder(50, 0, 10, 10));
		JPanel panelGeneral = new JPanel();

		model = new DefaultTableModel() {

		    public boolean isCellEditable(int row, int column)
		    {
		      return false;
		    }
		  }; 
		table = new JTable(model); 
		table.setToolTipText("Classement des pronostiqueurs");
		model.addColumn("Classement");
		model.addColumn("Pseudo"); 
		model.addColumn("Points"); 

		table.getColumnModel().getColumn(2).setPreferredWidth(10);
		scroll = new JScrollPane(table);
		scroll.setPreferredSize(new Dimension(250, 200));
		
		
		
		panelGeneral.add(scroll, BorderLayout.SOUTH);
		majContenu();
		this.add(panelGeneral);
		this.setVisible(true);	
	}
	
	public void majContenu() {
		ArrayList<Utilisateur> alUser = ihm.getControleur().getUtilisateurs();
		Collections.sort(alUser);
		int cpt=1;
		for(int i=0; i<nbLigneRemplies; i++)
			model.removeRow(0);
		
		nbLigneRemplies = 0;
		for(Utilisateur user : alUser) {
			model.addRow(new Object[]{cpt, user.getPseudo(), user.getNbPts()});
			nbLigneRemplies++;
			cpt++;
		}
		scroll.revalidate();
	}
}
